﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BasePlatformCommon;

namespace L9SubSet.WriteAddrPro
{
	public partial class Form_SelectRange : Form
	{
		// 最大分控索引，从 1 开始
		private int maxSubIndex = 0;

		private StartEnd startend = null;

		private string section = "SelectRange";			// 多语言文件配置节名称

		private string tip = "提示";						// MessageBox 题头

		/*****************************************************************************************/

		// 构造函数
		public Form_SelectRange(int maxSubIndex, StartEnd startend)
		{
			InitializeComponent();

			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
			this.Font = new System.Drawing.Font(this.Font.Name, 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

			this.maxSubIndex = maxSubIndex;
			this.startend = startend;

			startTextBox.Text = "1";
			endTextBox.Text = maxSubIndex.ToString();

			// 设置本地化语言
			if (LanguageOper.available == true)
			{
				setLanguageText();
			}
		}

		// 设置本地化语言
		public void setLanguageText()
		{
			if (LanguageOper.available == false)
			{
				return;
			}

			LanguageOper.SetCurrentCulture();

			tip = LanguageOper.ReadValue("Common", "tipStr");

			// 界面控件文字
			this.Text = LanguageOper.ReadValue(section, "selectRange.Text");
			fromLabel.Text = LanguageOper.ReadValue(section, "fromLabel.Text");
			toLabel.Text = LanguageOper.ReadValue(section, "toLabel.Text");
			okButton.Text = LanguageOper.ReadValue(section, "okButton.Text");
			cancelButton.Text = LanguageOper.ReadValue(section, "cancelButton.Text");
		}

		// 确定
		private void okButton_Click(object sender, EventArgs e)
		{
			int tmpStart = -1;
			int tmpEnd = -1;

			bool res = int.TryParse(startTextBox.Text.Trim(), out tmpStart);
			if (res == false || tmpStart < 1 || tmpStart > maxSubIndex)
			{
				string msg = string.Format("参数输入错误：起始分控应在 [{0},{1}] 范围", 1, maxSubIndex);
				if (LanguageOper.available == true)
				{
					msg = string.Format(LanguageOper.ReadValue(section, "msgStr.0"), 1, maxSubIndex);
				}
				
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
				
				return;
			}

			res = int.TryParse(endTextBox.Text.Trim(), out tmpEnd);
			if (res == false || tmpEnd < 1 || tmpEnd > maxSubIndex)
			{
				string msg = string.Format("参数输入错误：结束分控应在 [{0},{1}] 范围", 1, maxSubIndex);
				if (LanguageOper.available == true)
				{
					msg = string.Format(LanguageOper.ReadValue(section, "msgStr.1"), 1, maxSubIndex);
				}

				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
				
				return;
			}

			if (tmpStart <= tmpEnd)
			{
				startend.start = tmpStart - 1;
				startend.end = tmpEnd - 1;
			}
			else
			{
				startend.start = tmpEnd - 1;
				startend.end = tmpStart - 1;
			}

			this.DialogResult = System.Windows.Forms.DialogResult.OK;
		}

		// 取消
		private void cancelButton_Click(object sender, EventArgs e)
		{
			this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		}
	}
}
